package session;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * @Author Administrator
 * @Date 2020/6/4 15:49
 **/
@WebServlet("/checkCode")
public class CheckCode extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*1.创建图片对象 BufferedImage
          2.使用画笔 Graphics
           1、填充背景颜色
           2、画边框
           3、写字符
           4、画干扰线
          3、将图片输出到页面显示  ImageIO
        * */

        //设置图片宽高
        int width=120;
        int height=30;
        /*创建图片*/
        BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        /*设置画笔功能*/
        Graphics g=image.getGraphics();
        //画笔颜色
        g.setColor(Color.CYAN);
        /*填充背景色*/
        g.fillRect(0,0,width,height);
        g.setColor(Color.BLACK);
        g.drawRect(0,0,width-2,height-2);
        //随机数
        String s="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random=new Random();
        /*存储产生的四个字符*/
        StringBuilder sb=new StringBuilder();
        for (int i=0;i<0;i++){
            int index= random.nextInt(s.length());

            char ch=s.charAt(index);
            sb.append(ch);

        }
        g.setColor(Color.MAGENTA);
        g.drawString(sb.toString(),20,20);












        /*输出显示*/
        ImageIO.write(image,"jpg", resp.getOutputStream());
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
